const Service = require('egg').Service;

class LoginService extends Service {
	async demo(data) {
		// 1.增加数据
		var insertResult = await this.app.mongo.insertOne('users', {
			doc: { name: '李四', age: 20, sex: 1 }
		});

		// 2.删除数据
		var deleteResult = await this.app.mongo.findOneAndDelete('users', {
			filter: { name: '赵六' }
		});

		// 3.修改数据
		var updateResult = await this.app.mongo.findOneAndUpdate('users', {
			filter: { name: '王五' },
			update: {
				$set: { name: '赵六', age: 100 }
			}
		});

		// 4.查找数据
		var findResult = await this.app.mongo.find('users', {
			query: { name: '张三' },
			limit: 1
		});

		// 5.按ID查找的数据
		var findResultById = await this.app.mongo.find('users', {
			query: { _id: ObjectID('5b72abc7e4f0720a68f05b41') }
		});

		// 6.多表关联查询
		var aggregateResult = await this.app.mongo.aggregate('order', {
			pipeline: [
				{
					$lookup: {
						from: 'order_item',
						localField: 'order_id',
						foreignField: 'order_id',
						as: 'items'
					}
				},
				{
					$match: { all_price: { $gte: 90 } }
				},
				{
					$limit: 2
				}
			],
			// 其它的操作，如：是否为执行查询分析
			options: {}
		});
	}
	async login(data) {
        const { ctx, app } = this;
        const { password } = data;

        const res = await app.mongo.find('user', {});
        // const res = await this.app.mongo.insertOne('user', {
		// 	doc: { name: '李四', age: 20, sex: 1 }
		// });
		return res;
	}
}

module.exports = LoginService;
